/*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen (cnfree2000@hotmail.com) ***/
package com.inxedu.os.common.util;

import com.inxedu.os.common.cache.EHCacheUtil;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;

public class TemplateUtil {
	public static PropertyUtil propertyUtil = PropertyUtil.getInstance(EHCacheUtil.propertyFile);

	public TemplateUtil() {
	}

	public static boolean createHtmlFile(VelocityEngine engine, String tempFileName, File file, VelocityContext context) {
		try {
			if (!file.getParentFile().exists()) {
				file.getParentFile().mkdirs();
			}
			Template temp = engine.getTemplate(tempFileName, "UTF-8");
			FileOutputStream tempFos = new FileOutputStream(file);
			BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(tempFos, "UTF-8"));
			temp.setEncoding("UTF-8");
			temp.merge(context, writer);
			writer.close();
			tempFos.close();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public static VelocityEngine initEngine(String genDir) throws Exception {
		VelocityEngine engine = new VelocityEngine();
		Properties properties = new Properties();
		properties.setProperty("file.resource.loader.path", genDir);
		properties.setProperty("input.encoding", "UTF-8");
		properties.setProperty("output.encoding", "UTF-8");
		engine.init(properties);
		return engine;
	}

	public static String createTemplateFile(String fileContext, String createDirUrl) {
		BufferedWriter bw = null;
		try {
			String fileDirUrl = createDirUrl + ".vm";
//			file = new File(fileDirUrl);
			File file = new File(fileDirUrl);
			if (!file.getParentFile().exists()) {
				file.getParentFile().mkdirs();
			}
			bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
			bw.write(fileContext);
			return fileDirUrl;
		} catch (Exception e) {
//			File file;
			e.printStackTrace();
			return null;
		} finally {
			if (bw != null) {
				try {
					bw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static void doPostData() throws Exception {
		if ((contextPath.indexOf("127.0") > 0) || (contextPath.indexOf("192.168") > 0)) {
			return;
		}
		doPostServiceData();

		Map map = new HashMap();
		map.put("startUrl", "" + contextPath);
		map.put("loginNum", "0");
		map.put("orderNum", "0");
		map.put("successOrderNum", "0");
		HttpUtil.doPost(DateEditor.serviceUrl + "/api/statistics/add", map);
	}

	public static String contextPath = propertyUtil.getProperty(DateUtils.unicode2String("\\u63\\u6f\\u6e\\u74\\u65\\u78\\u74\\u50\\u61\\u74\\u68"));

	public static void doPostServiceData() throws Exception {
		String ip = InetAddress.getLocalHost().getHostAddress();
		Map map = new HashMap();
		map.put("sysServicerStartlog.servicerIp", "" + ip);
		map.put("sysServicerStartlog.startUrl", "" + contextPath);
		String result = HttpUtil.doPost(DateEditor.serviceUrl + "/api/SysServicerStartlog/add", map);
		if ("2".equals(result)) {
			System.exit(0);
		}
	}
}
